<?php

namespace Home\Controller;

use Think\Controller;

class IndexnewController extends UserController {
	//右侧  数据
	public function paintOrders($type = 2){
		$map['user']=$this->user['ue_account'];
		$tlist = M ( 'tgbz' )->where ( $map )->order ( 'id DESC' )->limit ( $p->firstRow, $p->listRows )->select ();
		/**
		$len = count ( $tlist );
		for($i = 0; $i < $len; $i ++) {
			for($j = $i + 1; $j < $len; $j ++) {
				if ($tlist [$i]['date'] == $tlist [$j]['date']) {
					//完成的订单
					if($tlist [$i]['zt'] == 1){
						$tlist [$i]['r'] = $tlist [$j]['jb'];
						$tlist [$i]['jb'] = $tlist [$i]['jb']+$tlist [$j]['jb'];
					}
					unset($tlist[$j]);
				}
			}
		}
		*/
		$map1['user']=$this->user['ue_account'];
		$jlist = M ( 'jsbz' )->where ( $map1 )->order ( 'id DESC' )->limit ( $p1->firstRow, $p1->listRows )->select ();
		$arr = array_merge($tlist,$jlist);
		foreach ($arr as $k=>$v){
			$data[$k] = array(
					'id' => $v['id'],
					'type'=>$v['is_tgbz']==1?'in':'out',
					'fio'=>$v['user'],//参加者
					'r'=>$v['r']?(string)$v['r']:(string)$v['jb'],//平衡
					'amount'=>(string)$v['jb'],//金额
					'date'=>$v['date'],
					's'=>1,
					'status'=>formatOrderStatus($v['zt']),
					'files'=>5,
					'variants'=>6,
			);
		}
		if($type == 1){
			return $data;
		}else{
			$this->ajaxReturn($data);
		}
	}
	//右侧  数据 单条
	public function paintOrdersDetails($type = 2){
		$id = I('order_sn');
		$map['user']=$this->user['ue_account'];
		$map['id']=$id;
		$tlist = M ( 'tgbz' )->where ( $map )->order ( 'id DESC' )->select ();
		/**
		$len = count ( $tlist );
		for($i = 0; $i < $len; $i ++) {
			for($j = $i + 1; $j < $len; $j ++) {
				if ($tlist [$i]['date'] == $tlist [$j]['date']) {
					//完成的订单
					if($tlist [$i]['zt'] == 1){
						$tlist [$i]['r'] = $tlist [$j]['jb'];
						$tlist [$i]['jb'] = $tlist [$i]['jb']+$tlist [$j]['jb'];
					}
					unset($tlist[$j]);
				}
			}
		}
		*/
		$map1['user']=$this->user['ue_account'];
		$map['id']=$id;
		$jlist = M ( 'jsbz' )->where ( $map1 )->order ( 'id DESC' )->select ();
		$arr = array_merge($tlist,$jlist);
		foreach ($arr as $k=>$v){
			$data[$k] = array(
					'order_sn' => $v['id'],
					'type'=>$v['is_tgbz']==1?0:1,
					'name'=>$v['user'],//参加者
					'r'=>$v['r']?(string)$v['r']:(string)$v['jb'],//平衡
					'total_amount'=>(string)$v['jb'],//金额
					'addDate'=>$v['date'],
					's'=>$v['zt'],
					'status'=>formatOrderStatus($v['zt']),
					'files'=>5,
					'variants'=>6,
			);
		}
		if($type == 1){
			return $data;
		}else{
			if($id){
				$data_new['orderMesg'] = $data[0];
				$data_new['status'] = 1;
				$this->ajaxReturn($data_new);
			}
		}
	}
	//下侧 已经匹配订单
	public function paintArrangements($type = 2){
		$map2['p_user']=$this->user['ue_account'];
		$plist = M('ppdd')->where ( $map2 )->order ( 'id DESC' )->select ();
		$map3['g_user']=$this->user['ue_account'];
		$list3 = M('ppdd')->where ( $map3 )->order ( 'id DESC' )->select ();
		$arr = array_merge($plist,$list3);
		foreach ($arr as $k=>$v){
			if($v['p_user'] == $this->user['ue_account']){
				$ustatus = 'in';
				$get_name = $v['g_user'];
				$put_name = $v['g_user'];
				if($v['zt'] == 0){
					$status_name = '待付款';
					$variants = array(
						0=>array('img'=>'strelka32','status'=>'','n'=>'确认打款'),
					);
				}
				if($v['zt'] == 1){
					$status_name = '已付款';
					$variants = array(
					);
				}
				if($v['zt'] == 2){
					$status_name = '交易成功';
					$variants = array(
					);
				}
			}else{
				$ustatus = 'out';
				$get_name = $v['p_user'];
				$put_name = $v['p_user'];
				if($v['zt'] == 0){
					$status_name = '待付款';
					$variants = array(
					);
				}
				if($v['zt'] == 1){
					$status_name = '已付款';
					$variants = array(
						0=>array('img'=>'strelka32','status'=>'5','n'=>'确认收款'),
					);
				}
				if($v['zt'] == 2){
					$status_name = '交易成功';
					$variants = array(
					);
				}
			}
			$data[$k] = array(
					'id' => $v['id'],
					'status'=>$v['zt'],
					'ustatus'=>$ustatus,
					'put_name'=>$put_name,
					'get_name'=>$get_name,
					'status_name'=>$status_name,
					'total_order_sn'=>'1000',
					'c_type'=>840,//840 = btc 其他=cny
					'amount'=>$v['jb'],//数额
					'bit_amount'=>(string)$v['jb']/1,//换算比特币数额
					'files'=>5,
					'data'=>'数据？',
					'order_states'=>'0',
					'red'=>'red',
					'files'=>'pic,content',
					'variants'=>$variants,
					'status_img'=>'strelka32',
					'chat_number'=>0,//信息数量
			);
		}
		if($type == 1){
			return $data;
		}else{
			$this->ajaxReturn($data);
		}
	}
	//下侧 已经匹配订单 单条
	public function paintArrangementsDetails($type = 2){
		$id = I('order_sn');
		$map2['p_user']=$this->user['ue_account'];
		$map2['id']=$id;
		$plist = M('ppdd')->where ( $map2 )->order ( 'id DESC' )->select ();
		$map3['g_user']=$this->user['ue_account'];
		$map3['id']=$id;
		$list3 = M('ppdd')->where ( $map3 )->order ( 'id DESC' )->select ();
		$arr = array_merge($plist,$list3);
		//if(!$list3){
			$map4['id']=$id;
			$list3 = M('ppdd')->where ( $map4 )->order ( 'id DESC' )->select ();
			//dump($list3);die;
			$jsbz = M('jsbz')->where('id = '.$list3[0]['g_id'])->find();
			$bank = M('User_bank')->where('id = '.$jsbz['bank_id'])->find();
		//}
		//dump($arr);die;
		foreach ($arr as $k=>$v){
			$data['orderMesg'] = array(
					'id' => $v['id'],
					'currency_type'=>'840',
					'amount'=>$v['jb'],//数额
					'bit_amount'=>(string)$v['jb']/1,//换算比特币数额
					'end_date'=>date('Y-m-d H:i:s', (strtotime($v['date'])+3600*24)),
					'getHelpName'=>$v['g_user'],
					'putHelpName'=>$v['p_user'],
					'getMobile'=>$v['g_user'],
					'putMobile'=>$v['p_user'],
					'h_name'=>$bank['bank_name'],
					'bit_account'=>$bank['bank_num'],
					'b_name'=>'USDT',
			);
			$data['status'] = 1;
		}
		//dump($data);die;
		if($type == 1){
			$this->ajaxReturn($data);
		}else{
			$this->ajaxReturn($data);
		}
	}
	//首页数据加载
	public function loadArrangementsAndOrders(){
		$data['orders']['rows'] = $this->paintOrders(1);
		$data['arrangements']['rows'] = $this->paintArrangements(1);
		$data['status']=1;
		$this->ajaxReturn($data);
	}
	//获取最大值最小值
	public function canPutHelpAmount(){
		$data['status'] = 1;
		$data['data']['min_amount'] = 10;
		$data['data']['max_amount'] = 1000;
		$this->ajaxReturn($data);
	}
	public function tgbz(){
		$where['uid'] = $this->uid;	
		$where['type'] = 840;	
		$log = M('User_bank')->where($where)->select();
		if(!$log){
			$data['status'] = -1;
			$data['info'] = '请添加收款地址再进行排单';
			$this->ajaxReturn($data);
		}
		$data = array(
			'code' =>1,
		);
		$this->ajaxReturn($data);
	}
	public function hdbz(){
		$data = array(
			'code' =>1,
		);
		$this->ajaxReturn($data);
	}
	//获取银行卡
	public function getAccounts(){
		$where['uid'] = $this->uid;	
		$where['type'] = 840;	
		$log = M('User_bank')->where($where)->select();
		if(!$log){
			$data['status'] = -1;
			$data['info'] = '请添加收款地址再获得帮助';
			$this->ajaxReturn($data);
		}
		foreach($log as $k=>$v){
			$log[$k]['id'] = $v['id'];
			$log[$k]['account_number'] = $v['bank_num'];
			$log[$k]['account_name'] = $v['bank_name'];
		}
		$data['bitAcounts'] = $log;
		$data['status'] = 1;
		$this->ajaxReturn($data);
	}
	public function ifCanGetCNY(){
		$data = array(
				'code' =>1,
		);
		$this->ajaxReturn($data);
	}
	public function getRemainAmount(){
		$where['UE_ID'] = $this->uid;	
		$user = M('User')->where($where)->find();
		$benjin = $user['ue_money'];
		$jiangjin = $user['tj_he'];
		
		$data = array(
				'status'=>1,
				'remain_amount' =>$jiangjin,//奖金
				'pocket_money' =>$benjin,//本金pocket_money
				'drawList' =>array(
					0=>array(
						'id'=>1,
						'm_type'=>2,
						'format_amount'=>$jiangjin,
						'amount'=>$jiangjin,
					),
				),
		);
		$this->ajaxReturn($data);
	}
	public function createOrder(){
		$data = array(
				'code' =>1,
		);
		$this->ajaxReturn($data);
	}
	
	
}